package com.xdxc.component.registry;

import com.xdxc.annotation.AnswerValidationRule;
import com.xdxc.component.validation.AnswerValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class ValidationRegistry {
    private final Map<Integer, AnswerValidator> validators = new ConcurrentHashMap<>();

    @Autowired
    public ValidationRegistry(List<AnswerValidator> validatorList) {
        validatorList.forEach(validator -> {
            AnswerValidationRule annotation = AnnotationUtils.findAnnotation(
                    validator.getClass(), AnswerValidationRule.class);
            if (annotation != null) {
                validators.put(annotation.quType(), validator);
            }
        });
    }

    public AnswerValidator getValidator(int quType) {
        return validators.get(quType);
    }
}
